package com.basis.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basis.entity.SysRole;
import com.basis.entity.SysRoleMenu;
import com.basis.mapper.SysRoleMapper;
import com.basis.mapper.SysRoleMenuMapper;
import com.basis.service.ISysMenuService;
import com.basis.service.ISysRoleService;

/**      
 * @Description:   系统角色表 服务实现类
 * @author:        lxd
 * @date:          2019年11月26日 下午4:42:00     
 */ 
@Service("sysRoleService")
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

	@Autowired
	private ISysMenuService sysMenuService;
	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;

	@Override
	public List<Map<String, Object>> queryMenus4Role(Integer id) {
		List<Map<String, Object>> ztree = sysMenuService.queryZtree();
		QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<SysRoleMenu>();
		queryWrapper.eq("role_id", id);
		List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(queryWrapper);
		for (SysRoleMenu sysRoleMenu : sysRoleMenus) {
			for (Map<String, Object> map : ztree) {
				if (map.get("pId").equals(0)) {
					map.put("open", true);
				}
				if (map.get("id").equals(sysRoleMenu.getMenuId())) {
					map.put("checked", true);
				}
			}
		}
		return ztree;
	}

	@Override
	public boolean saveMenus4Role(Integer id, String ids) {
		QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<SysRoleMenu>();
		queryWrapper.eq("role_id", id);
		this.sysRoleMenuMapper.delete(queryWrapper);
		for (String s : ids.split(",")) {
			SysRoleMenu sysRoleMenu = new SysRoleMenu();
			sysRoleMenu.setMenuId(Integer.valueOf(s));
			sysRoleMenu.setRoleId(id);
			sysRoleMenuMapper.insert(sysRoleMenu);
		}
		return true;
	}
}
